// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
  async userlistAction() {
    const page = this.post();
    const pages = page.pages;
    const limits = page.limits;
    const search = page.search;
    const zt = page.zt;
    const orid = this.userinfo.departmentid;
    const orcode = await this.model('or_department').where({id: orid}).getField('orcode', true);
    const dList = await this.model('or_department').where({orcode: orcode}).getField('id');

    const data = await this.model('or_app_user')
      .alias('u')
      .field('u.*,p.name as pname,z.name as zname, d.name as shopname, ds.name as bumenname')
      .join('think_or_position p ON u.positionid = p.id')
      .join('think_or_position z ON u.zhichengid = z.id')
      .join('think_or_department d on u.shopid = d.id')
      .join('think_or_department ds on u.departmentid = ds.id')
      .where({'u.departmentid': ['in', dList], 'u.isdisable': zt, 'u.name|u.username': ['like', '%' + search + '%'], 'u.isdelete': 0})
      .page(pages, limits)
      .order('u.id DESC')
      .countSelect();

    return this.json(data);
  }

  // 添加、编辑员工
  async addeditAction() {
    const data = this.post();
    const addtime = Math.round(new Date().valueOf() / 1000);

    const bumenInfo = await this.model('or_department').where({id: data.departmentid}).find();
    switch (bumenInfo.floor) {
      case 0:
        data.bumenid = 0;
        data.xiaozuid = 0;
        data.shopid = 0;
        data.departmentid = bumenInfo.id;
        break;
      case 1:
        data.bumenid = 0;
        data.xiaozuid = 0;
        data.shopid = bumenInfo.id;
        data.departmentid = bumenInfo.id;
        break;
      case 2:
        data.bumenid = bumenInfo.id;
        data.xiaozuid = 0;
        data.shopid = bumenInfo.pid;
        data.departmentid = bumenInfo.id;
        break;
      case 3:
        data.bumenid = bumenInfo.pid;
        data.xiaozuid = bumenInfo.id;
        data.shopid = await this.model('or_department').where({id: bumenInfo.pid}).getField('pid', true);
        data.departmentid = bumenInfo.id;
        break;
    }
    // 获取机构下所有部门及分店id
    const idlist = await this.model('or_department').where({orcode: bumenInfo.orcode}).getField('id');

    if (data.id * 1) {
      const chongfu = await this.model('or_app_user').where({isdelete: 0, username: data.username, departmentid: ['in', idlist], id: ['!=', data.id]}).select();
      if (!think.isEmpty(chongfu)) {
        return this.fail('该手机号码已经存在！');
      }
      data.password = think.md5(data.username);
      const gengxin = await this.model('or_app_user').update(data);
      return gengxin ? this.success('编辑成功') : this.fail('编辑失败');
    }

    const repeat = await this.model('or_app_user').where({isdelete: 0, username: data.username, departmentid: ['in', idlist]}).select();
    if (!think.isEmpty(repeat)) {
      return this.fail('该手机号码已经存在！');
    }

    data.password = think.md5(data.username);
    data.addtime = addtime;
    const res = await this.model('or_app_user').add(data);
    return res ? this.success('添加成功！') : this.fail('添加失败！');
  }

  async unstopAction() {
    const id = this.post('id');
    const st = this.post('st');
    const res = await this.model('or_app_user').where({id: id}).update({isdisable: st});
    if (st * 1) return res ? this.success('停用成功') : this.fail('停用失败');
    else return res ? this.success('复职成功') : this.fail('复职失败');
  }

  // 重置当前员工帐号和密码
  async resetpasswordAction() {
    const uid = this.get('id');
    const uinfo = await this.model('or_app_user').where({id: uid}).find();
    const password = think.md5(uinfo.username);
    const resetRes = await this.model('or_app_user').where({id: uid}).update({password: password});
    return resetRes ? this.success('重置员工密码成功') : this.fail('重置员工密码失败');
  }

  async getDepartmentListAction() {
    const orcode = await this.model('or_department').where({id: this.userinfo.departmentid}).getField('orcode', true);
    const list = await this.model('or_department').where({orcode: orcode}).select();
    return this.json(list);
  }

  // 获取员工信息
  async userAction() {
    const gid = this.get('gid');
    const data = await this.model('or_app_user')
      .alias('u')
      .field('u.*,p.name as pname,z.name as zname, d.name as shopname, ds.name as bumenname')
      .join('think_or_position p ON u.positionid = p.id')
      .join('think_or_position z ON u.zhichengid = z.id')
      .join('think_or_department d on u.shopid = d.id')
      .join('think_or_department ds on u.departmentid = ds.id')
      .where({'u.id': gid})
      .find();
    return this.json(data);
  }

  // 彻底删除员工
  async deleteUserAction() {
    const id = this.post().id;
    // 将id设置成弃用
    const res = await this.model('or_app_user').where({id: id}).update({isdelete: 1});
    return res ? this.success('操作成功') : this.error('操作失败');
  }
}
